@echo off
setlocal enabledelayedexpansion

:: 检查是否提供了必要的参数
if "%~1"=="" goto :MissingFileName
if "%~2"=="" goto :MissingMaxLines

:: 设置参数
set "InputFile=%~1"
set /a "MaxLines=%~2"

echo File Name: %InputFile%
echo Split every %MaxLines% lines

:: 从输入文件名中提取基本名称（不含扩展名）
for %%i in ("%InputFile%") do set "BaseName=%%~ni"

:: 检查文件是否存在
if not exist "%InputFile%" goto :FileNotFound

:: 初始化变量
set /a LineCount=0
set /a PartCount=1
set "OutputFile=!BaseName!_!PartCount!.txt"
echo Appending to %OutputFile%

:: 使用for命令逐行读取文件
for /f "delims=" %%a in ('type "%InputFile%"') do (
    set /a LineCount+=1
    if !LineCount! gtr !MaxLines! (
        set /a LineCount=0
        set /a PartCount+=1
        set "OutputFile=!BaseName!_!PartCount!.txt"
        echo Appending to !OutputFile!
    )
    echo %%a>>"!OutputFile!"
)

echo Done splitting the file.
goto :EOF

:: 错误处理
:MissingFileName
echo Error: No input file name provided.
goto :EOF

:MissingMaxLines
echo Error: No maximum line count provided.
goto :EOF

:FileNotFound
echo Error: The file '%InputFile%' was not found.
goto :EOF
